#include <stdlib.h>
#include <string.h>
#include <pthread.h>

#include <minigui/common.h>
#include <minigui/minigui.h>

#include "gs.h"
#include "setup.h"

#if 0
static void debug_setup(void)
{
    int i;

    printf ("g_gs.code = %s\n", g_gs.code);
    printf ("g_gs.name = %s\n", g_gs.name);
    printf ("g_gs.tanks_nr = %d\n", g_gs.tanks_nr);


    for (i=0;i<g_gs.tanks_nr; i++) {
        printf ("\n============Tank #%d infos============\n", i);
        printf ("tank[%d].tank_id = %d\n", i, g_gs.tanks[i].tank_id);
        printf ("tank[%d].height = %d\n", i, g_gs.tanks[i].height);
        printf ("tank[%d].oil_id = %d\n", i, g_gs.tanks[i].oil_id);
        printf ("tank[%d].density = %f\n", i, g_gs.tanks[i].density);
        printf ("tank[%d].addr = %d\n", i, g_gs.tanks[i].addr);
        printf ("tank[%d].oil_b = %d\n", i, g_gs.tanks[i].oil_b);
        printf ("tank[%d].water_b = %d\n", i, g_gs.tanks[i].water_b);
        printf ("tank[%d].table = %s\n", i, g_gs.tanks[i].table);
        printf ("tank[%d].o_upper = %d\n", i, g_gs.tanks[i].o_upper);
        printf ("tank[%d].o_lower = %d\n", i, g_gs.tanks[i].o_lower);
        printf ("tank[%d].w_upper = %d\n", i, g_gs.tanks[i].w_upper);
        printf ("tank[%d].w_lower = %d\n", i, g_gs.tanks[i].w_lower);
        printf ("tank[%d].t_upper = %d\n", i, g_gs.tanks[i].t_upper);
        printf ("tank[%d].t_lower = %d\n", i, g_gs.tanks[i].t_lower);

    }

    printf ("user = %s\n", g_gs.user);
    printf ("password = %s\n", g_gs.password);
}
#endif

static void init_file(void)
{
    int i;

    strcpy (g_gs.code, "0000");
    strcpy (g_gs.name, "北京华美特有限公司");
    g_gs.tanks_nr = 4;

    for (i=0; i<g_gs.tanks_nr; i++) {
        GS_TANK *tank = &g_gs.tanks[i];
        tank->tank_id = i + 1;
        tank->height = 3000;
        tank->oil_id = 93;
        tank->density = 0.75;
        tank->addr = 0x80 + i;
        tank->oil_b = 0;
        tank->water_b = 0;
        tank->o_uupper = 2800;
        tank->o_upper = 2600;
        tank->o_lower = 1500;
        tank->o_llower = 1000;
        tank->w_upper = 200;
        tank->w_lower = 0;
        tank->t_upper = 40;
        tank->t_lower = -40; 
        tank->state = -1;
        sprintf(tank->table, "table/table%d.hc", tank->tank_id);
    }

    sprintf(g_gs.user, "%d", 100);
    sprintf(g_gs.password, "%d", 100);
    g_gs.leak_time = 3600; // second
    g_gs.leak_value = 10; // v
    // debug_setup();
}

static int write_file(FILE *fp)
{
    int ret;

    ret = fwrite(&g_gs, sizeof(g_gs), 1, fp);

    return (ret == sizeof(GS));
}

static int read_file(FILE *fp)
{
    int i, ret;

    ret = fread(&g_gs, sizeof(g_gs), 1, fp);
    
    for (i=0; i<g_gs.tanks_nr;i++) {
        g_gs.tanks[i].oil = 0;
        g_gs.tanks[i].water = 0;
        g_gs.tanks[i].temp = 0;
        g_gs.tanks[i].t1 = 0;
        g_gs.tanks[i].t2 = 0;
        g_gs.tanks[i].t3 = 0;
        g_gs.tanks[i].t4 = 0;
        g_gs.tanks[i].t5 = 0;
    }
     // debug_setup();

    return (ret == sizeof(GS));
}

int gs_setup_save(void)
{
    FILE* fp;
    int ret;
    
    fp = fopen("./gs.cfg", "w");
    
    printf("g_gs.tanks_nr = %d\n", g_gs.tanks_nr);

    ret = write_file(fp);

    fclose (fp);

    return ret;
}

int gs_setup_init(void)
{
    FILE* fp;
    int ret;
    int i;
 
    for (i=0; i<16; i++) {
        pthread_mutex_init (&g_gs.tanks[16].lock, NULL);
    }

    fp = fopen("./gs.cfg", "r");
    if (fp == NULL) {
        init_file();
        gs_setup_save();
        return -1;
    }

    ret = read_file(fp);

    fclose (fp);
    return ret;
}
